<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core" 
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">

    <ui:composition template="../../templates/creatively-cabinet.xhtml">

        <ui:param name="pageTitle" value="Home / File Download"/>
        <ui:param name="webSiteTitle" value="X Files"/>

        <ui:define name="submenu">
        </ui:define>

        <ui:define name="content">
            <h:form id="create_request_form" rendered="#{downloadRequestBean.canDownload}"> 
                You are about to download file ##{downloadRequestBean.fileId}.
                <h:panelGrid class="table" columns="2">
                    <h:outputText value="File Name:"/> 
                    <h:outputText value="#{downloadRequestBean.file.name}"/>

                    <h:outputText value="Description:"/>
                    <h:outputText value="#{downloadRequestBean.file.description}"/>

                    <h:outputText value="Content-type:"/>
                    <h:outputText value="#{downloadRequestBean.file.contentType}"/>

                    <h:outputText value="Uploaded by:"/>
                    <h:outputText value="#{downloadRequestBean.file.createdBy.name} #{downloadRequestBean.file.createdBy.surname}"/>

                    <h:outputText value="File Size (kbytes):"/>
                    <h:outputText value="#{downloadRequestBean.file.size/(1024*1024)}">
                        <f:convertNumber pattern="#0.00"/>
                    </h:outputText>

                    <h:outputText value="Checksum (CRC32):"/> <h:outputText value="#{downloadRequestBean.file.crc}"/>
                </h:panelGrid>
                <h:panelGroup id="request_download_controls">
                    <h:panelGrid columns="2">
                        <h:outputText value="Password" rendered="#{downloadRequestBean.passwordNeeded}"/>
                        <h:inputSecret value="#{downloadRequestBean.key}" rendered="#{downloadRequestBean.passwordNeeded}"/>

                        <h:commandLink id="request_download_btn" actionListener="#{downloadRequestBean.createRequest}" 
                                       class="button_green" value="Request Download" 
                                       onclick="$('#request_download_controls').css('display', 'none');">
                            <f:ajax execute="@form" onevent="handleRequestDownload"/>
                        </h:commandLink>
                    </h:panelGrid>
                </h:panelGroup>
            </h:form>
            <h:panelGroup rendered="#{!downloadRequestBean.canDownload}" class="grid_8">
                <ul class="message">
                    <li class="message_bad">
                        <h:panelGroup rendered="#{!downloadRequestBean.fileExists}">The requested file does not exists.</h:panelGroup>
                        <h:panelGroup rendered="#{downloadRequestBean.fileExists}">You don't have permissions to download this file.</h:panelGroup>
                    </li>
                </ul>
            </h:panelGroup>
            <script type="text/javascript">
                function handleRequestDownload(e) {
                    if(e.status === 'success')
                        document.location = '#{request.contextPath}/faces/cabinet/home.xhtml#;tab;requests';
                }
            </script>
        </ui:define>

    </ui:composition>

</html>